#!/bin/sh

print_help() {
    echo "Usage: $0 [--release] [--debug] [--cflags] [--libs] [--gl] [--gles] [--prefix=[/some/path]]"
    echo "  --release: indicate to compile/link for release"
    echo "  --debug: indicate to compile/link for debug"
    echo "  --cflags: print compile flags"
    echo "  --libs: print link flags"
    echo "  --static: modifier to indicate statically linking against FastUIDraw"
    echo "  --nodirs: exclude directory directives of FastUIDraw install location"
    echo "  --libdir=/some/path: specify library directory"
    echo "  --incdir=/some/path: specify include directory"
    echo "  --gl: use GL API (includes flags to use NGL dispatch)"
    echo "  --gles: use GLES API (includes flags to use NGL dispatch)"
    echo "  --ngl: add flags to use NGL dispatch"
    echo "  --ngles: add flags to use NGLES dispatch"
    echo "  --prefix: print path to which fastuidraw was installed"
    echo "  --prefix=/some/path: specify the path to which fastuidraw was installed"
    echo "Notes: Never mix release and debug in a single executable, they are NOT ABI compatible"
    echo "       if both --release and --debug options are placed, all but the last of those is ignored"
    echo "       if both --gl and --gles options are placed, all but the last of those is ignored"
}

#if ! options=$(getopt -o- -l release,debug,cflags,libs,gl,gles,help -- "$@"); then
#    print_help
#    exit 1
#fi

if test $# -eq 0; then
      print_help
      exit 1
fi

mode=unknown

static_linking=0

print_cflags=0
print_libs=0
print_dirs=1

add_gl=0
add_gles=0
add_ngl=0
add_ngles=0

printhelp=0
location="@INSTALL_LOCATION@"

lib_location=$location/lib
include_location=$location/include

while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case "$1" in
      --prefix=*)
	  location=$optarg
	  lib_location=$location/lib
	  include_location=$location/include
	  ;;
      --libdir=*)
	  lib_location=$optarg
	  ;;
      --incdir=*)
	  include_location=$optarg
	  ;;
      --prefix) echo $location ;;
      --release) mode=release ;;
      --debug) mode=debug ;;
      --cflags) print_cflags=1 ;;
      --libs) print_libs=1 ;;
      --nodirs) print_dirs=0 ;;
      --gles) add_gles=1 add_ngles=1 ;;
      --gl) add_gl=1 add_ngl=1 ;;
      --GLES) add_gles=1 add_ngles=1 ;;
      --GL) add_gl=1 add_ngl=1 ;;
      --ngl) add_ngl=1 ;;
      --ngles) add_ngles=1 ;;
      --static) static_linking=1 ;;
      --help) printhelp=1 ;;
      *)
        print_help
        exit 1 ;;
  esac
  shift
done

cflags=
base_cflags=

libs=
dir_libs=

if [ "$print_dirs" = "1" ]; then
    base_cflags=-I$include_location
    dir_libs=-L$lib_location
fi

if [ "$printhelp" = "1" ]; then
    print_help
    exit 1
fi

if [ "$print_cflags" = "1" ] || [ "$print_libs" = "1" ]; then
  bad=0
  if [ "$mode" = "unknown" ]; then
      echo "Must specify debug or release (with --debug or --release respectively)"
      bad=1
  fi

  if [ "$add_ngl" = "1" ] && [ "$add_ngles" = "1" ]; then
      echo "Cannot mix GL and GLES dispatch in the same application"
      bad=1
  fi

  if [ "$bad" = 1 ]; then
      exit 1
  fi
fi

libs="$dir_libs -lFastUIDraw_$mode"
cflags="$base_cflags"

if [ "$mode" = "release" ]; then
    cflags="$cflags @FASTUIDRAW_release_CFLAGS@"
else
    cflags="$cflags @FASTUIDRAW_debug_CFLAGS@"
fi

if [ "$add_ngl" = "1" ]; then
    libs="$libs -lNGL_$mode"
fi

if [ "$add_ngles" = "1" ]; then
    libs="$libs -lNGLES_$mode"
fi

if [ "$add_gl" = "1" ]; then
    libs="$libs -lFastUIDrawGL_$mode"
    cflags="$cflags @FASTUIDRAW_GL_CFLAGS@"
fi

if [ "$add_gles" = "1" ]; then
    libs="$libs -lFastUIDrawGLES_$mode"
    cflags="$cflags @FASTUIDRAW_GLES_CFLAGS@"
fi

if [ "$print_cflags" = "1" ]; then
    echo $cflags
fi

if [ "$static_linking" = "1" ]; then
    pre_link="-Wl,--whole-archive,-Bstatic"
    post_link="-Wl,--no-whole-archive -static-libstdc++ -static-libgcc"
    base_libs="@FASTUIDRAW_DEPS_STATIC_LIBS@"
else
    pre_link=
    post_link=
    base_libs="@FASTUIDRAW_DEPS_LIBS@"
fi

if [ "$print_libs" = "1" ]; then
    echo $base_libs $pre_link $libs $post_link
fi
